package com.siyoumi.app.modules.sms.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.siyoumi.app.entity.SysSms;
import com.siyoumi.app.entity.SysStock;
import com.siyoumi.app.modules.account.service.SvcAccount;
import com.siyoumi.app.modules.sms.service.SvcSms;
import com.siyoumi.component.XBean;
import com.siyoumi.component.http.InputData;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.controller.AdminApiController;
import com.siyoumi.entity.SysAccount;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.service.SysAccountService;
import com.siyoumi.util.XReturn;
import com.siyoumi.validator.XValidator;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/xadmin/sms/sms__list")
public class sms__list
        extends AdminApiController {
    @GetMapping()
    public XReturn index() {
        setPageTitle("短信列表");

        String apiType = XHttpContext.getXConfig().getAconfig_api_type();
        XValidator.isNullOrEmpty(apiType, "未配置站点接口类型参数");

        InputData inputData = InputData.fromRequest();

        String[] select = {
                "sms_id",
                "sms_name",
                "sms_sign_name",
                "sms_template_code",
                "stock_count_left",
                "stock_count_use",
        };
        JoinWrapperPlus<SysSms> query = SvcSms.getBean().listQuery(inputData);
        query.join(SysStock.table(), SysSms.tableKey(), "stock_id_src");
        query.eq("sms_api_type", XHttpContext.getXConfig().getAconfig_api_type());
        query.select(select);

        IPage<SysSms> page = new Page<>(inputData.getPageIndex(), inputData.getPageSize());
        //list
        IPage<Map<String, Object>> pageData = SvcSms.getApp().getMaps(page, query);
        List<Map<String, Object>> list = pageData.getRecords();
        long count = pageData.getTotal();

        for (Map<String, Object> item : list) {
            SysSms entityAcc = XBean.fromMap(item, SysSms.class);

            item.put("id", entityAcc.getKey());
        }

        getR().setData("list", list);
        getR().setData("count", count);

        return getR();
    }


    //删除
    @Transactional
    @PostMapping("/del")
    public XReturn del() {
        return SvcSms.getBean().delete(getIds());
    }
}
